
#include "imtPublic.h"
#include "imtSkeletonAnimationDataManager.h"
#include "imtXmlParser.h"
#include "imtLog.h"

namespace iMate
{
//----------------------------------------------------------------------------
SkeletonAnimationDataManager::SkeletonAnimationDataManager()
{
	ms_Singleton = this;
}
//----------------------------------------------------------------------------
SkeletonAnimationDataManager::~SkeletonAnimationDataManager()
{
	//mCreaterMap.clear();
	mActions.clear();

}
//----------------------------------------------------------------------------
bool SkeletonAnimationDataManager::load(const char* filename)
{
	SkeletonAnimationDatasMap::iterator iter = mActions.find(filename);
	if( iter != mActions.end()  )
	{
		return true;
	}
	else
	{
		String strFile = String(filename) + String(".xml");
		DataStreamPtr stream = ResourceGroupManager::getSingleton().openResource(strFile);
		if(stream.isNull())	return false;
		XmlSerializer serializer;
		XmlDocumentPtr doc = serializer.parse(stream.get());
		if( doc.isNull() )
			return false;
		else
		{
			MapSkeletonAnimationDataList datamap;
			iMate::XmlElement* element = doc.get()->FirstChildElement("SkeletonAnimations");
			
			if( element != NULL )
			{
				SkeletonAnimationData data;
				XmlElement *item = element->FirstChildElement("Animation");
				while( NULL != item )
				{
					int mask = -1;
					int level = -1;
					String name;
					imtXmlQueryAttrib(item,"name",name);
					imtXmlQueryAttribHex<int,std::ios_base::hex>(item,"mask",mask);
					imtXmlQueryAttrib<int>(item,"level",level);
					data.mMask = DWORD(mask);
					data.mLevel = SkeletonAnimPriorityLevel(level);
					datamap.insert(std::make_pair(name,data));
					item = item->NextSiblingElement("Animation");

				}
				mActions[filename] = datamap;
			}
			else
				return false;
		}
		return true;
	}
	
	
}
//----------------------------------------------------------------------------
//SkeletonAnimatonData* SkeletonAnimationDataManager::createSkeletonAction(const Ogre::String &type)
//{
//	MapSkeletonActionCreaterList::iterator iter = mCreaterMap.find(type);
//	if( iter != mCreaterMap.end() )
//	{
//		creater func = (*iter).second;
//		if( func )
//		{
//			SkeletonAction* action = func();
//			return action;
//		}
//	}
//	return NULL;
//}
//----------------------------------------------------------------------------
//void SkeletonAnimationDataManager::RegisterCreater()
//{
//	mCreaterMap.insert(std::make_pair("SkeletonPrimaryAction",(creater)(&SkeletonPrimaryAction::CreateObject)));
//	mCreaterMap.insert(std::make_pair("SkeletonHighLevelAction",(creater)(&SkeletonHighLevelAction::CreateObject)));
//}
//----------------------------------------------------------------------------
const SkeletonAnimationData* SkeletonAnimationDataManager::getByName(const Ogre::String& skeleton,const Ogre::String& name) const
{
	SkeletonAnimationDatasMap::const_iterator iter = mActions.find(skeleton);
	if( iter != mActions.end() )
	{
		MapSkeletonAnimationDataList::const_iterator animIter = (*iter).second.find(name);
		if( animIter != (*iter).second.end() )
			return &(( *animIter).second );
	}	
	return NULL;
}


	SkeletonAnimationDataManager& SkeletonAnimationDataManager::getSingleton(void)
	{
		assert( ms_Singleton );  return ( *ms_Singleton );
	}
	SkeletonAnimationDataManager* SkeletonAnimationDataManager::getSingletonPtr(void)
	{
		return ms_Singleton;
	}
}

namespace Ogre {

	template<> iMate::SkeletonAnimationDataManager* Singleton<iMate::SkeletonAnimationDataManager>::ms_Singleton = NULL;
}